package com.flying.acls.domain.context;

import com.flying.acls.model.ResourceType;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

@Slf4j
public class DOExpressionContextBuilder {
    private ContextStringValueResolver resolver;

    public DOExpressionContextBuilder(ContextStringValueResolver resolver) {
        this.resolver = resolver;
    }

    public ExpressionContext build(Object principal, Object domainObject) {
        ExpressionContext context = new ExpressionContext(domainObject, principal, ResourceType.DO);
        context.addEnvVar(resolver.getResourceClassName(), domainObject.getClass().getName());
        return context;
    }

    /**
     * todo: consider to load the object from store.
     */
    public ExpressionContext build(Object principal, Serializable targetId, String targetType) {
        ExpressionContext context = new ExpressionContext(null, principal, ResourceType.DO);
        context.addEnvVar(resolver.getResourceTag() + resolver.getTagSeparator() + resolver.getResourceIdProperty(), String.valueOf(targetId));
        context.addEnvVar(resolver.getResourceClassName(), targetType);
        return context;
    }
}